<!-- 领用出库管理 -->
<template>
  <div class="mod-config">
    <el-button-group style="margin-bottom: 10px;">
      <el-button icon="el-icon-search" type="primary" @click="searchHandle">查询</el-button>
      <el-button icon="el-icon-delete" type="primary" @click="searchReset">清空</el-button>
      <el-button v-if="permissions.eims_takeinventory_add" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle()">新增</el-button>
    </el-button-group>
    <el-form inline label-width="100px" @keyup.enter.native="searchHandle">
      <el-form-item label="单据编号">
        <el-input v-model="dataForm.takeBillId"></el-input>
      </el-form-item>
      <el-form-item label="制造商">
        <el-input v-model="dataForm.supplyName" placeholder="请选择">
          <el-button slot="append" @click="chooseSupply">选择</el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="商品品种">
        <el-input v-model="dataForm.brandName" placeholder="请选择">
          <el-button slot="append" @click="chooseBrand">选择</el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="发出仓位">
        <el-select v-model="dataForm.outStockId" clearable>
          <el-option v-for="item in stockOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态">
        <el-select v-model="dataForm.isAudit" placeholder="请选择" clearable>
          <el-option v-for="item in isAuditOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="领用类型">
        <el-select v-model="dataForm.receptionType" placeholder="请选择" clearable>
          <el-option v-for="item in receptionTypeOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="领用部门">
        <el-select v-model="dataForm.receptionId" placeholder="请选择" v-if="dataForm.receptionType == 1" clearable>
          <el-option v-for="item in receptionDeptOptions" :value="item.id" :label="item.name" :key="item.id">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
  <div class="avue-crud">
    <el-table :data="dataList" border v-loading="dataListLoading" :max-height="tableHeight">
      <el-table-column label="操作" width="280" fixed="right">
        <template slot-scope="scope">
          <a :href="printUrl" target="_blank" style="margin-right: 15px;">
            <el-button type="text" icon="el-icon-printer" @click="getPrintParams(scope.row.takeBillId)">打印</el-button>
          </a>
          <el-button type="text" icon="el-icon-search" @click="showDetails(scope.row.id)" :disabled="scope.row.isAudit == 0">详情</el-button>
          <el-button v-if="permissions.eims_takeinventory_edit" type="text" icon="el-icon-edit" @click="audit(scope.row.id)"
            :disabled="scope.row.isAudit == 1">审核</el-button>
          <el-button v-if="permissions.eims_takeinventory_edit" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id)"
            :disabled="scope.row.isAudit == 1">修改</el-button>
          <el-button v-if="permissions.eims_takeinventory_del" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row.id)"
            :disabled="scope.row.isAudit == 1">删除</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="takeBillId" label="单据编号" width="140"></el-table-column>
      <el-table-column prop="createTime" label="单据日期" width="130"></el-table-column>
      <el-table-column prop="receptionName" label="领用部门/客户" width="100"></el-table-column>
      <el-table-column prop="isAudit" label="审核状态" :formatter="isAuditType" width="80"></el-table-column>
      <el-table-column prop="createUsername" label="制单人" width="80"></el-table-column>
      <el-table-column prop="auditUsername" label="审核人" width="80"></el-table-column>
      <el-table-column prop="memo" label="备注" show-overflow-tooltip></el-table-column>

    </el-table>
  </div>
  <div class="avue-crud__pagination">
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex" :page-sizes="[10, 20, 50, 100, 200]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next"></el-pagination>
  </div>
  <detail-table v-if="detailsVisible" ref="details" @refreshDataList="getDataList"></detail-table>
  <add-table v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-table>
  <supply-search v-if="supplyVisible" ref="supplySearch" @refreshData="updateSearchForm"></supply-search>
  <search-brand v-if="brandVisible" ref="brandSearch" @refreshData="updateBrandForm"></search-brand>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { filterParams } from '@/util/tools'
  import { getStockList } from '@/api/eims/stock'
  import { getDeptInfo } from '@/api/admin/dept'
  import {
    fetchGetRecipientsList,
    auditRecipientsOfOutbound,
    delObj
  } from "@/api/eims/take-inventory/get-recipients"

  import detailTable from "./details"
  import AddTable from "./add"
  import SupplySearch from './supply-search'
  import SearchBrand from '@/components/business/SearchBrand'

  export default {
    data() {
      return {
        printUrl: '',
        dataForm: {
          supplyName: '',
          brandName: ''
        },
        dataList: [],
        receptionTypeOptions: [{label: '领用部门', value: 1},{label: '客户领用',value: 2}],
        stockOptions: [],
        isAuditOptions: [{label: '已审核',value: 1},{label: '未审核',value: 0}],
        receptionDeptOptions: [],
        pageIndex: 1,
        pageSize: 200,
        totalPage: 0,
        dataListLoading: false,
        detailsVisible: false,
        supplyVisible: false,
        brandVisible: false,
        addOrUpdateVisible: false
      };
    },
    created() {
      this.getDataList();
      this.stockList();
      this.getDeptList();
    },
    computed: {
      ...mapGetters(['permissions', 'bizData']),
      tableHeight: function(){
        return window.innerHeight - 260
      }
    },
    components: {
      detailTable,
      SupplySearch,
      SearchBrand,
      AddTable
    },
    methods: {
      //  查询数据
      searchHandle() {
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true;
        let params1 = filterParams(this.dataForm);
        let params2 = {
          current: this.pageIndex,
          size: this.pageSize
        };
        fetchGetRecipientsList(Object.assign(params2, params1)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false
        });
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val;
        this.pageIndex = 1;
        this.getDataList();
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val;
        this.getDataList();
      },
      chooseSupply() {
        this.supplyVisible = true
        this.$nextTick(() => {
          this.$refs.supplySearch.init()
        })
      },
      chooseBrand() {
        this.brandVisible = true;
        if (!this.dataForm.supplyId) {
          this.$message.warning("请选择制造商")
          return
        }
        this.$nextTick(() => {
          this.$refs.brandSearch.init(this.dataForm)
        })
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      //审核
      audit(id) {
        this.$confirm('是否审核ID为' + id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return auditRecipientsOfOutbound(id)
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success('审核成功')
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 删除
      deleteHandle(id) {
        this.$confirm('是否确认删除ID为' + id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delObj(id)
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success('删除成功')
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 查看详情
      showDetails(id) {
        this.detailsVisible = true;
        this.$nextTick(() => {
          this.$refs.details.init(id);
        });
      },
      //  打印
      getPrintParams(id) {
        let params = '/storage_takeReceiveRpt.cpt&billId='
        this.printUrl = this.bizData.printUrl + params + id
      },
      //  获取仓位列表
      stockList() {
        getStockList().then(response => {
          if(response.data.code == '0'){
            this.stockOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      getDeptList() {
        getDeptInfo().then(response => {
          if(response.data.code == '0'){
            this.receptionDeptOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        });
      },
      updateSearchForm(data) {
        this.dataForm.supplyId = data.id
        this.dataForm.supplyName = data.name

      },
      updateBrandForm(data) {
        this.dataForm.brandId = data.id
        this.dataForm.brandName = data.name
      },
      //清空查询列表
      searchReset() {
        for (let key in this.dataForm) {
          this.dataForm[key] = ''
        }
      },
      isAuditType(row, column, cellValue, index) {
        switch (cellValue) {
          case 0:
            return '未审核'
            break
          case 1:
            return '已审核'
            break
        }
      },
    }
  };
</script>
